#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;


int Search(char * s)
{
    int i=0;
    for (i=0;i<6;i++)
    {
        if(keyword[i][0]==s[0])
        {
            return i+1;
        }
    }
}
void lexical(FILE *fp) {
	char x = getc(fp);
	while(x==' '||x=='\n'||x=='\t')
	{	
        	x=getc(fp);
	}
	char temp='\0';
    	int i=0;
	num = 0;
	strcpy(id, "\0");
	if((x>='a'&&x<='z')||(x>='A'&&x<='Z')) 
    {
        id[i++]=x;
        x=getc(fp);
        while((x>'a'&&x<'z')||(x>'A'&&x<'Z')||(x>'0'&&x<'9'))
        {
            id[i++]=x;
            x=getc(fp);
        }
        id[i]='\0';
        ungetc(x,fp);
        if(id[0]=='d'&&id[1]=='o')
            token=6;
        else if(id[0]=='f'&&id[1]=='o'&&id[2]=='r')
            token=1;
        else if(id[0]=='i'&&id[1]=='f')
            token=2;
        else if(id[0]=='t'&&id[1]=='h'&&id[2]=='e'&&id[3]=='n')
            token=3;
		else if(id[0]=='e'&&id[1]=='l'&&id[2]=='s'&&id[3]=='e')
			token=4;
        else if(id[0]=='w'&&id[1]=='h'&&id[2]=='i'&&id[3]=='l'&&id[4]=='e')
            token=5;
        else
            token=10;
    }
    else if(x>='0'&&x<='9')
    {
        id[i++]=x;
        x=getc(fp);
        if((x>='a'&&x<='z')||(x>='A'&&x<='Z'))
        {
            printf("error\n");
            return;
        }
        while(x>='0'&&x<='9')
        {
           id[i++]=x;
           x=getc(fp);
        }
        id[i]='\0';
        ungetc(x,fp);
        token=11;
        return;
    }
	else
    {
        if(x==':')
        {
            id[i++]=':';
            x=getc(fp);
            if(x=='=')
            {
                id[i++]='=';
                id[i]='\0';
                token=18;
                return;
            }
            else
            {
                ungetc(x,fp);
                token=17;
                return;
            }
        }
        else if(x=='<')
        {

            id[i++]='<';
            x=getc(fp);
            if(x=='>')
            {
                id[i++]='>';
                id[i]='\0';
                token=21;
                return;
            }
            else if(x=='=')
            {
                id[i++]='=';
                id[i]='\0';
                token=22;
                return;
            }
            else
            {
                ungetc(x,fp);
                id[i]='\0';
                token=20;
            }
        }
        else if(x=='>')
        {
            id[i++]='>';
            x=getc(fp);
            if(x=='=')
            {
                id[i++]='=';
                id[i]='\0';
                token=24;
                return;
            }
            else
            {

                ungetc(x,fp);
                id[i]='\0';
                token=23;
                return;
            }
        }
        else if(x=='+')
        {
            token=13;
            id[0]='+';
            id[1]='\0';
            return;
        }
        else if(x==';')
        {
            token=26;
            id[0]=';';
            id[1]='\0';
            return;
        }
        else if(x=='-')
        {
            token=14;
            id[0]='-';
            id[1]='\0';
            return;
        }
        else if(x=='*')
        {

            token=15;
            id[0]='*';
            id[1]='\0';
            return ;
        }
        else if(x=='/')
        {

            token=16;
            id[0]='/';
            id[1]='\0';
            return;
        }
        else if(x=='=')
        {

            token=25;
            id[0]='=';
            id[1]='\0';
            return;
        }

        else if(x=='(')
        {
            token=27;
            id[0]='(';
            id[1]='\0';
            return;
        }
        else if(x==')')
        {

            token=28;
            id[0]=')';
            id[1]='\0';
            return;
        }
        else if(x=='#')
        {

            token=0;
            id[0]='#';
            id[1]='\0';
            return;
        }
    }


}



int main(int argc, const char *argv[])
{
	if(argc < 2) {
		printf("insufficient args\n");
		return 1;
	}
	FILE *fp = fopen(argv[1], "r");
	do {
		lexical(fp);
		switch(token) {
			case 10: printf("(id, %s)\n", id); break;
			case 11: printf("(num, %s)\n", id); break;
			case -1: printf("error!\n"); break;
			default: printf("(%d, %s)\n", token, id);
		}
	} while(token != 0);
	fclose(fp);
	return 0;
}
